package com.sunny.shop.service;

import com.sunny.shop.bo.VaccineBO;
import com.sunny.shop.core.LocalUser;
import com.sunny.shop.model.Vaccination;
import com.sunny.shop.repository.VaccinationRepository;
import com.sunny.shop.repository.VaccineRepository;
import com.sunny.shop.vo.VaccineVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class VaccineServiceImpl {
    @Autowired
    private VaccineRepository vaccineRepository;
    @Autowired
    private VaccinationRepository vaccinationRepository;
    public HashMap<Long, VaccineVO> getVaccine(){
        Integer status = 1;
        Long uid = LocalUser.getUser().getId();
        List<com.sunny.shop.model.Vaccine> result = vaccineRepository.findAllByStatus(status);
        ArrayList<Long> existId = new ArrayList();
        List<Vaccination> list = vaccinationRepository.findAllByStatusAndUid(status, uid);
        list.forEach(value->{
            Long vaccineId = value.getVaccineId();
            existId.add(vaccineId);
        });
        // 返回的结果
        HashMap<Long, VaccineVO> res = new HashMap<>();
        result.forEach(item->{
            Integer tagStatus = 0;
            if(existId.contains(item.getId())) tagStatus = 1;
            VaccineVO v = new VaccineVO();
            ArrayList<VaccineBO> boList = new ArrayList<>();
            VaccineBO vaccineBO = new VaccineBO();
            BeanUtils.copyProperties(item, vaccineBO);
            vaccineBO.setStatus(tagStatus);
            v.setInject(item.getVaccinationDate()); // 最里层Inject
            if(res.containsKey(item.getGroupId())){
                ArrayList<VaccineBO> org = res.get(item.getGroupId()).getInjectName();
                org.add(vaccineBO);
                v.setInjectName(org);  // 最里层InjectName
            }else{
                boList.add(vaccineBO);
                v.setInjectName(boList);  // 最里层InjectName
            }
            res.put(item.getGroupId(), v);
        });


        return res;
    }
}
